import { exec } from "node:child_process";
import { writeFileSync } from "node:fs";
import dayjs from "dayjs";

exec("git log --all", (error, stdout, stderr) => {
    if (error) {
        console.error(`Error: ${error.message}`);
        return;
    }

    const list = stdout.split("\n");
    let logs = [];
    for (let i = 0; i < list.length; i++) {
        const element = list[i];
        const log = list[i + 2]?.trim();
        if (element.startsWith("Date:") && log.startsWith("docs:")) {
            const date = dayjs(element.replace("Date:", "")).format(
                "YYYY-MM-DD HH:mm"
            );
            logs.push("-   " + date + " " + log.replace("docs:", "").trim());
        }
    }

    writeFileSync("./log.md", "# 更新日志\n\n" + logs.join("\n"));
});
